package dkbta.ontology.elements.pattern.conditions;

import java.util.Calendar;

import dkbta.ontology.elements.Element;
import dkbta.ontology.values.NumericValues;
import dkbta.ontology.values.ValuesType;
import dkbta.util.time.TimeInterval;
import dkbta.util.time.TimeUnit;

public class BoundPointCondition{
	/** The values the constraint imposes on the start time */
	private final NumericValues _startValues;

	/** The calendar time unit the start time values refer to */
	private final TimeUnit _startTimeUnit;

	/** The values the constraint imposes on the end time */
	private final NumericValues _endValues;

	/** The calendar time unit the end time values refer to */
	private final TimeUnit _endTimeUnit;

	/**
	 * @param startValues The values the constraint imposes on the start time
	 * @param startTimeUnit The calendar time unit the start time values refer to
	 * @param endValues The values the constraint imposes on the end time
	 * @param endTimeUnit The calendar time unit the end time values refer to
	 */
	public BoundPointCondition(NumericValues startValues, TimeUnit startTimeUnit,
		NumericValues endValues, TimeUnit endTimeUnit){
		_startValues = startValues;
		_startTimeUnit = startTimeUnit;
		_endValues = endValues;
		_endTimeUnit = endTimeUnit;
	}

	/**
	 * Checks whether a certain instance conforms to the condition
	 * 
	 * @param e The instance
	 * @return Whether the certain instance conforms to the condition
	 */
	public boolean checkCondition(Element e){
		TimeInterval timeInterval = e.getTimeInterval();
		Calendar start = timeInterval.getStartTimeAsCalendar();
		Calendar end = timeInterval.getEndTimeAsCalendar();
		boolean check = true;

		// Checking the start time constraint if such exists
		check = check && (_startValues != null) ? _startValues.isValid(
			ValuesType.NUMERIC, start.get(_startTimeUnit.calendarTimeUnit)) : true;

		// Checking the end time constraint if such exists
		check = check
				&& ((_endValues != null) ? _endValues.isValid(ValuesType.NUMERIC, end
						.get(_endTimeUnit.calendarTimeUnit)) : true);

		return check;
	}

	/**
	 * Returns a string representation of the constraint
	 * 
	 * @return A string representation of the constraint
	 */
	@Override
	public String toString(){
		StringBuilder sb = new StringBuilder();
		sb.append("Start [");
		sb.append(_startTimeUnit);
		sb.append(", ");
		sb.append(_startValues);
		sb.append("], End [");
		sb.append(_endTimeUnit);
		sb.append(", ");
		sb.append(_endValues);
		sb.append("]");
		return sb.toString();
	}
}
